package com.thingple.usb;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetDialog;

public class UsbDialog {

    private final Context context;

    private final View.OnClickListener listener;

    private BottomSheetDialog dialog;
    public UsbDialog(Context context, View.OnClickListener listener) {
        this.context = context;
        this.listener = listener;
        init();
    }

    private void init() {

        dialog = new BottomSheetDialog(this.context);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(R.layout.device_loading_layout);
        View view = dialog.getDelegate().findViewById(com.google.android.material.R.id.design_bottom_sheet);
        if (view != null) {
            view.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
        }
        setContent("检测到读卡器连接");
        Button cancelBtn = dialog.findViewById(R.id.btn_cancel);
        if (cancelBtn != null) {
            cancelBtn.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onClick(v);
                }
            });
        }
    }

    public void show() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public void onDestroy() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    public void setContent(String content) {
        if (dialog != null) {
            TextView statusLabel = dialog.findViewById(R.id.label_status);
            if (statusLabel != null) {
                statusLabel.setText(content);
            }
        }
    }
}
